<?php
class Category extends Eloquent {

	protected $table = 'categories';
	
	/**
     * get all categories where delete = false
     * @author Thuan Truong
     */
    public function getAllCategory () {
        $category = Category::where('is_deleted', '=', 0)->get();
        return $category;
    }
	
    
    /**
     * count all categories where delete = false
     * @author Thuan Truong
     */
    public function countAllCategory () {
        $category = Category::where('is_deleted', '=', 0)->count();
        return $category;
    }
    
	/**
     * find category by id
     * @author Thuan Truong 
     */
    public function findCategoryById ($id) {
    	return $this->whereRaw('id = ? and is_deleted = ?', array($id, 0))->first();
    }
    
    
    /**
     * Validate input create, update category
     * @author Thuan Truong
     * @param input
     * @return array
     */
    public function validate($input) {
        $rules = array(
            'en_title' => 'required|max:250',
            'vi_title' => 'required|max:250',
        );
        $validator = Validator::make($input, $rules);
        return $validator;
    }
    
    /**
     * Create category
     * @author Thuan Truong
     * @param input
     */
    public function createCategory($input) {
        $category = new Category;
		$category->parent_id = $input['parent'];
        $category->name_en = $input['en_title'];
        $category->description_en = $input['en_content'];  
        $category->name_vi = $input['vi_title'];
        $category->description_vi = $input['vi_content'];      
        $category->status = 0;
        $category->save();
        
    }

    /**
     * Update Category
     * @author Thuan Truong
     */
    public function updateCategory($input, $id) {
        $category = Category::find($id); 
		$category->parent_id = $input['parent'];
        $category->name_en = $input['en_title'];
        $category->description_en = $input['en_content'];  
        $category->name_vi = $input['vi_title'];
        $category->description_vi = $input['vi_content'];      
        $category->update();
        
    }
	
	/**
     * delete category
     * @author Thuan Truong
     */
    public function deleteCategory($id) {
        DB::table('categories')
            ->where('id', $id)
            ->update(array('is_deleted' => 1));
    }
}